﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ARR.Entities
{
    public partial class HistoryDetail
    {
        public string MC { get; set; }

        public byte Level { get; set; }

        public decimal? SS_ActualMarginCY
        {
            get
            {
                if (!SS_GIMCY.HasValue)
                {
                    return null;
                }
                else
                {
                    return (SS_GIMCY ?? 0) + (SS_PIDiffCY ?? 0) + (SS_ChargeOffCY ?? 0) + (SS_RebatesCY ?? 0);
                }
            }
        }

        public decimal? SS_GIMPCY
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_GIMCY.HasValue)
                {
                    return null;
                }

                if (SS_SalesCY != 0)
                {
                    return SS_GIMCY / SS_SalesCY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_PIDiffPCY
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_PIDiffCY.HasValue)
                {
                    return null;
                }

                if (SS_SalesCY != 0)
                {
                    return SS_PIDiffCY / SS_SalesCY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_ChargeOffPCY
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_ChargeOffCY.HasValue)
                {
                    return null;
                }

                if (SS_SalesCY != 0)
                {
                    return SS_ChargeOffCY / SS_SalesCY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_RebatesPCY
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_RebatesCY.HasValue)
                {
                    return null;
                }

                if (SS_SalesCY != 0)
                {
                    return SS_RebatesCY / SS_SalesCY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_ActualMarginPCY
        {
            get
            {
                if (!SS_GIMPCY.HasValue)
                {
                    return null;
                }
                else
                {
                    return SS_GIMPCY + (SS_PIDiffPCY ?? 0) + (SS_ChargeOffPCY ?? 0) + (SS_RebatesPCY ?? 0);
                }
            }
        }


        public decimal? SS_ActualMarginPY
        {
            get
            {
                if (!SS_GIMPY.HasValue)
                {
                    return null;
                }
                else
                {
                    return (SS_GIMPY ?? 0) + (SS_PIDiffPY ?? 0) + (SS_ChargeOffPY ?? 0) + (SS_RebatesPY ?? 0);
                }
            }
        }

        public decimal? SS_GIMPPY
        {
            get
            {
                if (!SS_SalesPY.HasValue || !SS_GIMPY.HasValue)
                {
                    return null;
                }

                if (SS_SalesPY != 0)
                {
                    return SS_GIMPY / SS_SalesPY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_PIDiffPPY
        {
            get
            {
                if (!SS_SalesPY.HasValue || !SS_PIDiffPY.HasValue)
                {
                    return null;
                }

                if (SS_SalesPY != 0)
                {
                    return SS_PIDiffPY / SS_SalesPY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_ChargeOffPPY
        {
            get
            {
                if (!SS_SalesPY.HasValue || !SS_ChargeOffPY.HasValue)
                {
                    return null;
                }

                if (SS_SalesPY != 0)
                {
                    return SS_ChargeOffPY / SS_SalesPY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_RebatesPPY
        {
            get
            {
                if (!SS_SalesPY.HasValue || !SS_RebatesPY.HasValue)
                {
                    return null;
                }

                if (SS_SalesPY != 0)
                {
                    return SS_RebatesPY / SS_SalesPY.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public decimal? SS_ActualMarginPPY
        {
            get
            {
                if (!SS_GIMPPY.HasValue)
                {
                    return null;
                }
                else
                {
                    return SS_GIMPPY + (SS_PIDiffPPY ?? 0) + (SS_ChargeOffPPY ?? 0) + (SS_RebatesPPY ?? 0);
                }
            }
        }


        public decimal? SS_VolumeVsPY
        {
            get
            {
                if (SS_VolumeCY == null || SS_VolumePY == null)
                {
                    return null;
                }

                decimal ss_volumePY = Convert.ToDecimal(SS_VolumePY);
                decimal ss_volumeCY = Convert.ToDecimal(SS_VolumeCY);

                if (ss_volumePY != 0)
                {
                    return Math.Abs(ss_volumeCY) / ss_volumePY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_SalesVsPY
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_SalesPY.HasValue)
                {
                    return null;
                }

                if (SS_SalesPY != 0)
                {
                    return Math.Abs(SS_SalesCY.Value) / SS_SalesPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_GIMVsPY
        {
            get
            {
                if (!SS_GIMCY.HasValue || !SS_GIMPY.HasValue)
                {
                    return null;
                }

                if (SS_GIMPY != 0)
                {
                    return Math.Abs(SS_GIMCY.Value) / SS_GIMPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_PIDiffVsPY
        {
            get
            {
                if (!SS_PIDiffCY.HasValue || !SS_PIDiffPY.HasValue)
                {
                    return null;
                }

                if (SS_PIDiffPY != 0)
                {
                    return Math.Abs(SS_PIDiffCY.Value) / SS_PIDiffPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_ChargeOffVsPY
        {
            get
            {
                if (!SS_ChargeOffCY.HasValue || !SS_ChargeOffPY.HasValue)
                {
                    return null;
                }

                if (SS_ChargeOffPY != 0)
                {
                    return Math.Abs(SS_ChargeOffCY.Value) / SS_ChargeOffPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_RebatesVsPY
        {
            get
            {
                if (!SS_RebatesCY.HasValue || !SS_RebatesPY.HasValue)
                {
                    return null;
                }

                if (SS_RebatesPY != 0)
                {
                    return Math.Abs(SS_RebatesCY.Value) / SS_RebatesPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_ActualMarginVsPY
        {
            get
            {
                if (!SS_ActualMarginCY.HasValue || !SS_ActualMarginPY.HasValue)
                {
                    return null;
                }

                if (SS_ActualMarginPY != 0)
                {
                    return Math.Abs(SS_ActualMarginCY.Value) / SS_ActualMarginPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_GIMPVsPY
        {
            get
            {
                if (!SS_GIMPCY.HasValue || !SS_GIMPPY.HasValue)
                {
                    return null;
                }

                if (SS_GIMPPY != 0)
                {
                    return Math.Abs(SS_GIMPCY.Value) / SS_GIMPPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_ActualMarginPVsPY
        {
            get
            {
                if (!SS_ActualMarginPCY.HasValue || !SS_ActualMarginPPY.HasValue)
                {
                    return null;
                }

                if (SS_ActualMarginPPY != 0)
                {
                    return Math.Abs(SS_ActualMarginPCY.Value) / SS_ActualMarginPPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_InventoryVsPY
        {
            get
            {
                if (!SS_InventoryCY.HasValue || !SS_InventoryPY.HasValue)
                {
                    return null;
                }

                if (SS_InventoryPY != 0)
                {
                    return Math.Abs(SS_InventoryCY.Value) / SS_InventoryPY - 1;
                }
                else
                {
                    return 1;
                }
            }
        }


        public decimal? SS_VolumeVsAverage
        {
            get
            {
                if (SS_VolumeCY == null || AverageVolume == null)
                {
                    return null;
                }


                decimal averageVolume = Convert.ToDecimal(AverageVolume);
                decimal ss_volumeCY = Convert.ToDecimal(SS_VolumeCY);

                if (averageVolume != 0)
                {
                    return Math.Abs(ss_volumeCY) / averageVolume - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_SalesVsAverage
        {
            get
            {
                if (!SS_SalesCY.HasValue || !this.AverageSales.HasValue)
                {
                    return null;
                }

                if (AverageSales != 0)
                {
                    return Math.Abs(SS_SalesCY.Value) / AverageSales - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_GIMVsAverage
        {
            get
            {
                if (!SS_GIMCY.HasValue || !AverageGIM.HasValue)
                {
                    return null;
                }

                if (AverageGIM != 0)
                {
                    return Math.Abs(SS_GIMCY.Value) / AverageGIM - 1;
                }
                else
                {
                    return 1;
                }
            }
        }

        public decimal? SS_GIMPVsAverage
        {
            get
            {
                if (!SS_GIMPCY.HasValue || !AverageGIMP.HasValue)
                {
                    return null;
                }

                if (AverageGIMP != 0)
                {
                    return Math.Abs(SS_GIMPCY.Value) / AverageGIMP - 1;
                }
                else
                {
                    return 1;
                }
            }
        }


        public decimal? Space
        {
            get
            {
                return (Gondola ?? 0)
                       + (TransactionArea ?? 0)
                       + (GrabAndGo ?? 0)
                       + (WIC ?? 0)
                       + (OpenCooler ?? 0)
                       + (Freezer ?? 0)
                       + (Forecourt ?? 0)
                       + (OtherExhibition ?? 0);
            }
        }


        public decimal? InventoryDays
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SS_GIMCY.HasValue)
                {
                    return null;
                }
                else
                {
                    if ((SS_SalesCY - SS_GIMCY) != 0)
                    {
                        return Math.Abs(SS_InventoryCY ?? 0) / (SS_SalesCY - SS_GIMCY) / 30;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public decimal? AnnualStockTurn
        {
            get
            {
                if (!ActiveCategories.HasValue || ActiveCategories != 1 || !InventoryDays.HasValue)
                {
                    return null;
                }
                else
                {
                    if (InventoryDays != 0)
                    {
                        return 365 / InventoryDays;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public decimal? GMROI
        {
            get
            {
                if (!SS_GIMCY.HasValue || !SS_InventoryCY.HasValue)
                {
                    return null;
                }
                else
                {
                    if (SS_InventoryCY != 0)
                    {
                        return Math.Abs(SS_GIMCY.Value) / SS_InventoryCY;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public decimal? SalesVsLinearSpace
        {
            get
            {
                if (!SS_SalesCY.HasValue || !Space.HasValue)
                {
                    return null;
                }
                else
                {
                    if (Space != 0)
                    {
                        return Math.Abs(SS_SalesCY.Value) / Space;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public decimal? GMROS
        {
            get
            {
                if (!SS_GIMCY.HasValue || !Space.HasValue)
                {
                    return null;
                }
                else
                {
                    if (Space != 0)
                    {
                        return Math.Abs(SS_GIMCY.Value) / Space;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }
        
        public decimal? AveragePrice
        {
            get
            {
                if (!SS_SalesCY.HasValue || SS_VolumeCY == null)
                {
                    return null;
                }
                else
                {
                    decimal ss_volumeCY = Convert.ToDecimal(SS_VolumeCY);

                    if (ss_volumeCY != 0)
                    {
                        return Math.Abs(SS_SalesCY.Value) / ss_volumeCY;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public decimal? HurdleRateRatio
        {
            get
            {
                if (!SS_SalesCY.HasValue || !SKUsWithSales.HasValue)
                {
                    return null;
                }
                else
                {
                    if (SKUsWithSales.HasValue && SKUsWithSales.Value != 0)
                    {
                        return (SS_SalesCY ?? 0) / (SKUsWithSales ?? 0);
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }


        public int? TotalSKUs
        {
            get
            {
                if (!SKUsWithSales.HasValue && !NewSKUs.HasValue)
                {
                    return null;
                }
                else
                {
                    return (SKUsWithSales ?? 0) + (NewSKUs ?? 0);
                }
            }
        }

        public int? SKUsAfterRR
        {
            get
            {
                if (!TotalSKUs.HasValue)
                {
                    return null;
                }
                else
                {
                    return TotalSKUs - (DelistProposal ?? 0);
                }
            }
        }


        public decimal? SKUsWith80SalesP
        {
            get
            {
                if (!Sales80.HasValue || !Sales100.HasValue)
                {
                    return null;
                }

                if (Sales100 != 0)
                {
                    return (decimal)Sales80 / Sales100;
                }
                else
                {
                    return null;
                }
            }
        }



    }
}
